﻿using HalconDotNet;
using Newtonsoft.Json;
using NLog;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LeaderCCSLaserUI.Models.VisionModels
{
    public class VM_TwoLineIntersection : FunctionBase
    {
        public override bool Run(ref HObject ho_Image, string paramJson, ref HTuple hv_Row, ref HTuple hv_Column, ref HTuple hv_Angle, ref HTuple hv_Score, ref HObject maxRegion, ref HObject ho_SelectedRegion, ref HTuple v1, ref HTuple v2)
        {
            try
            {
                var MTwoLineIntersectionParam = JsonConvert.DeserializeObject<TwoLineIntersectionParam>(paramJson);
                HOperatorSet.GetImageSize(ho_Image, out var hv_Width, out var hv_Height);
                HTuple hv_LineParameter1 = new HTuple(), hv_LineParameter2 = new HTuple();
                HObject ho_line1, ho_line2;
                HOperatorSet.GenEmptyObj(out ho_line1);
                HOperatorSet.GenEmptyObj(out ho_line2);
                HOperatorSet.VectorAngleToRigid(MTwoLineIntersectionParam.ST_Row, MTwoLineIntersectionParam.ST_Column, 0, hv_Row, hv_Column, 0, out var homMad2D);
                {
                    HOperatorSet.CreateMetrologyModel(out var hv_MetrologyHandle);
                    HOperatorSet.SetMetrologyModelImageSize(hv_MetrologyHandle, hv_Width, hv_Height);
                    HOperatorSet.AffineTransPoint2d(homMad2D, MTwoLineIntersectionParam.L1_StartRow, MTwoLineIntersectionParam.L1_StartColumn, out var startRow, out var startColumn);
                    HOperatorSet.AffineTransPoint2d(homMad2D, MTwoLineIntersectionParam.L1_EndRow, MTwoLineIntersectionParam.L1_EndColumn, out var endRow, out var endColumn);
                    HOperatorSet.AddMetrologyObjectLineMeasure(hv_MetrologyHandle, startRow, startColumn, endRow, endColumn, MTwoLineIntersectionParam.L1_MeasureLength1, MTwoLineIntersectionParam.L1_MeasureLength2, MTwoLineIntersectionParam.L1_MeasureSigma, MTwoLineIntersectionParam.L1_MeasureThreshold, new HTuple(),
                        new HTuple(), out var hv_Index);
                    HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandle, hv_Index, "num_instances",
                        1);
                    HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandle, hv_Index, "measure_transition",
                        MTwoLineIntersectionParam.L1_MeasureTransition.ToString());
                    HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandle, hv_Index, "measure_select",
                        MTwoLineIntersectionParam.L1_MeasureSelect.ToString());
                    HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandle, hv_Index, "min_score",
                        MTwoLineIntersectionParam.L1_MinScore);
                    HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandle, hv_Index, "num_measures",
                        MTwoLineIntersectionParam.L1_NumMeasures);
                    HOperatorSet.ApplyMetrologyModel(ho_Image, hv_MetrologyHandle);
                    //HOperatorSet.GetMetrologyObjectMeasures(out var contours, hv_MetrologyHandle, "all", "all", out var Row, out var Column);
                    HOperatorSet.GetMetrologyObjectResult(hv_MetrologyHandle, hv_Index, "all", "result_type",
                        "all_param", out hv_LineParameter1);
                    HOperatorSet.GenRegionLine(out ho_line1, hv_LineParameter1.TupleSelect(0), hv_LineParameter1.TupleSelect(1), hv_LineParameter1.TupleSelect(2), hv_LineParameter1.TupleSelect(3));
                    //HOperatorSet.GetMetrologyObjectResultContour(out ho_contour1, hv_MetrologyHandle, 0, "all", 1.5);
                    HOperatorSet.ClearMetrologyModel(hv_MetrologyHandle);
                }
                {
                    HOperatorSet.CreateMetrologyModel(out var hv_MetrologyHandle);
                    HOperatorSet.SetMetrologyModelImageSize(hv_MetrologyHandle, hv_Width, hv_Height);
                    HOperatorSet.AffineTransPoint2d(homMad2D, MTwoLineIntersectionParam.L2_StartRow, MTwoLineIntersectionParam.L2_StartColumn, out var startRow, out var startColumn);
                    HOperatorSet.AffineTransPoint2d(homMad2D, MTwoLineIntersectionParam.L2_EndRow, MTwoLineIntersectionParam.L2_EndColumn, out var endRow, out var endColumn);
                    HOperatorSet.AddMetrologyObjectLineMeasure(hv_MetrologyHandle, startRow, startColumn, endRow, endColumn, MTwoLineIntersectionParam.L2_MeasureLength1, MTwoLineIntersectionParam.L2_MeasureLength2, MTwoLineIntersectionParam.L2_MeasureSigma, MTwoLineIntersectionParam.L2_MeasureThreshold, new HTuple(),
                        new HTuple(), out var hv_Index);
                    HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandle, hv_Index, "num_instances",
                        1);
                    HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandle, hv_Index, "measure_transition",
                        MTwoLineIntersectionParam.L2_MeasureTransition.ToString());
                    HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandle, hv_Index, "measure_select",
                        MTwoLineIntersectionParam.L2_MeasureSelect.ToString());
                    HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandle, hv_Index, "min_score",
                        MTwoLineIntersectionParam.L2_MinScore);
                    HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandle, hv_Index, "num_measures",
                        MTwoLineIntersectionParam.L2_NumMeasures);
                    HOperatorSet.ApplyMetrologyModel(ho_Image, hv_MetrologyHandle);
                    //HOperatorSet.GetMetrologyObjectMeasures(out var contours, hv_MetrologyHandle, "all", "all", out var Row, out var Column);
                    HOperatorSet.GetMetrologyObjectResult(hv_MetrologyHandle, hv_Index, "all", "result_type",
                        "all_param", out hv_LineParameter2);
                    HOperatorSet.GenRegionLine(out ho_line2, hv_LineParameter2.TupleSelect(0), hv_LineParameter2.TupleSelect(1), hv_LineParameter2.TupleSelect(2), hv_LineParameter2.TupleSelect(3));
                    //HOperatorSet.GetMetrologyObjectResultContour(out ho_contour2, hv_MetrologyHandle, 0, "all", 1.5);
                    HOperatorSet.ClearMetrologyModel(hv_MetrologyHandle);
                }
                HOperatorSet.Union2(ho_line1, ho_line2, out ho_SelectedRegion);
                HOperatorSet.IntersectionLines(hv_LineParameter1.TupleSelect(0), hv_LineParameter1.TupleSelect(1), hv_LineParameter1.TupleSelect(2), hv_LineParameter1.TupleSelect(3),
                    hv_LineParameter2.TupleSelect(0), hv_LineParameter2.TupleSelect(1), hv_LineParameter2.TupleSelect(2), hv_LineParameter2.TupleSelect(3),out hv_Row,out hv_Column,out var isOverlapping);
                HOperatorSet.AngleLx(hv_LineParameter1.TupleSelect(0), hv_LineParameter1.TupleSelect(1), hv_LineParameter1.TupleSelect(2), hv_LineParameter1.TupleSelect(3), out hv_Angle);
                return true;
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                return false;
            }
        }
    }
}
